package Day36.test2;

/**
 * 栈
 */
public class Stack {

    private Integer[] nums;
    private int index = -1;//栈顶下标
    private int size=0;//栈中元素的数量
    public Stack(int size){
        nums = new Integer[size];
    }

    /**
     * 入栈
     * @param element
     */
    public void push(int element){
        index++;
        nums[index] = element;
        size++;
    }

    /**
     * 出栈
     * @return
     */
    public int pop(){
        if(size==0){
            throw new RuntimeException("空栈无法取栈顶元素");
        }
        int num = nums[index];
        nums[index] = null;
        index--;
        size--;
        return num;
    }

    /**
     * 读取栈顶元素
     * @return
     */
    public int peak(){
        if(size==0){
            throw new RuntimeException("空栈，无法读取栈顶元素");
        }
        int num = nums[index];
        return num;
    }


    public int size(){
        return this.size;
    }

}
